package com.sx.micowell.module.setting

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.rx.RxBus
import com.example.mvvm.until.SPUtils
import com.gyf.immersionbar.ImmersionBar
import com.example.mvvm.bar.TitleBar
import com.example.mvvm.until.AppUtils
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragSettingsBinding
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.SettingModel
import com.sx.micowell.framework.tools.AppDialogUtils
import com.sx.micowell.framework.tools.EventInfo

/**
 * @author: YY
 * @time: 2021/8/9
 * @describe 设置列表
 */
@Route(path = RoutePath.SETTING)
class SettingFragment : MyFragment<FragSettingsBinding, ContainerActivity>(), TitleBarAction {

    lateinit var viewModel: SettingModel

    override fun initViewModel() {
        viewModel = getFragmentScopeViewModel(SettingModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_settings, BR.viewModel, viewModel)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
        viewModel.currentVersion.value=AppUtils.getAppVersionName()
    }

    override fun initData() {
        viewModel.calculateCacheSize()

    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.currentVersion.observe(this) {
            binding.settingCertification.setRightText(it)
        }

        viewModel.loginSwitchEvent.observe(this, {
            myActivity.finish()
        })

        viewModel.updateVersionEvent.observe(this, {
            if (it.appversion > AppUtils.getAppVersion()) {
                AppDialogUtils.showUpdateDialog(
                    context, it.appversion.toString(), it.remark,
                    object : AppDialogUtils.IOnClickListener {
                        override fun leftListener() {}
                        override fun rightListener() {

                        }
                    })
            }

        })

        // 缓存数据
        viewModel.cacheSizeEvent.observe(this, { data ->
            binding.settingAboutUs.rightText = data
        })

        // 清除缓存
        viewModel.clearCacheEvent.observe(this, {
            AppDialogUtils.showBasicDialog(
                context, "提示", "确定要清除系统缓存吗？",
                object : AppDialogUtils.IOnClickListener {
                    override fun leftListener() {}
                    override fun rightListener() {
                        viewModel.clearCache()
                    }
                })
        })

        // 退出登录
        viewModel.loginOutEvent.observe(this, {
            AppDialogUtils.showBasicDialog(
                context, "提示", "确定要退出登录？",
                object : AppDialogUtils.IOnClickListener {
                    override fun leftListener() {}
                    override fun rightListener() {
                        viewModel.loginOut(false)
                    }
                })
        })


        // 退出成功
        viewModel.loginOutStateEvent.observe(this, {
            // 清除token、手机号
            SPUtils.getInstance().put(SPKey.SP_TOKEN, "")
            SPUtils.getInstance().put(SPKey.SP_PHONE, "")
            RxBus.getDefault().post(EventInfo(AppConstant.EVENT_LOGIN_OUT))
            mActivity.finish()
        })

    }

    override fun getTitleBar(): TitleBar {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity.finish()
    }
}